package com.wiz.netty.message;

import java.io.UnsupportedEncodingException;

import com.google.protobuf.ByteString;
import com.wiz.common.NettyConstant;
import com.wiz.data.factory.MessageDataFactory;
import com.wiz.data.vo.to.DataMessageTemplate;
import com.wiz.data.vo.to.MabDataTO;
import com.wiz.data.vo.type.MessageType;

/**
 * 消息工厂，根据参数获取不同的发送报文
 * @author Hakeny
 */
public class NettyMessageFactory implements NettyConstant{
	private static NettyMessageFactory instance;
	private static Object obj = new Object();
	
	private MessageDataFactory messageDataFactory = MessageDataFactory.getInstance();
	
	private NettyMessageFactory(){}

	public static NettyMessageFactory getInstance() {
		if (instance != null) {
			return instance;
		}
		synchronized (obj) {
			if (instance != null) {
				return instance;
			}
			instance = new NettyMessageFactory();
		}
		return instance;
	}

	/**
	 * 构建发送心跳报文
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public NettyMessageTO.MessageContent getIdleMessage() throws UnsupportedEncodingException {
		NettyMessageTO.MessageContent.Builder builder = NettyMessageTO.MessageContent.newBuilder();
		builder.setType(MessageType.IDLE.getId());
		builder.setIden(ByteString.copyFrom(SERVER_IDEN,"utf-8"));
		builder.setMab(ByteString.copyFrom(getNowTime()));
		return builder.build();
	}
	
	/**
	 * 获取当前时间的8位byte
	 * @return
	 */
	private byte[] getNowTime(){
		long longValue = System.currentTimeMillis();
		byte[] b = new byte[8]; 
        for (int i = 0; i < b.length; i++) { 
            b[i] = new Long(longValue & 0xff).byteValue();// 将最低位保存在最低位 
            longValue = longValue >> 8; // 向右移8位 
        }
        return b;
	}
	
	/**
	 * 构建发送数据报文
	 * @param mabDataTO
	 * @param messageName
	 * @param dynamicKey
	 * @return
	 * @throws Exception
	 */
	public NettyMessageTO.MessageContent getDataMessage(MabDataTO mabDataTO,DataMessageTemplate dmTemplate,String dynamicKey) throws Exception {
		byte[] mti = messageDataFactory.getMtiMessage(dmTemplate);
		byte[] mab = messageDataFactory.getMabMessage(mabDataTO, dmTemplate);
		byte[] key = dynamicKey.getBytes();//动态密钥
		byte[] mac = messageDataFactory.getMac(mab, key);
		
		NettyMessageTO.MessageContent.Builder builder = NettyMessageTO.MessageContent.newBuilder();
		builder.setType(MessageType.DATA.getId());
		builder.setIden(ByteString.copyFrom(SERVER_IDEN,"utf-8"));
		builder.setMti(ByteString.copyFrom(mti));
		builder.setMab(ByteString.copyFrom(mab));
		builder.setMac(ByteString.copyFrom(mac));
		return builder.build();
	}
	
	/**
	 * 构建发送签到报文应答
	 * @param mab
	 * @param mac
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public NettyMessageTO.MessageContent getSignMessage(byte[] mti,byte[] mab,byte[] mac) throws UnsupportedEncodingException {
		NettyMessageTO.MessageContent.Builder builder = NettyMessageTO.MessageContent.newBuilder();
		builder.setType(MessageType.SIGN.getId());
		builder.setIden(ByteString.copyFrom(SERVER_IDEN,"utf-8"));
		builder.setMti(ByteString.copyFrom(mti));
		builder.setMab(ByteString.copyFrom(mab));
		builder.setMac(ByteString.copyFrom(mac));
		return builder.build();
	}

}
